home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / comms / other / amarquee / examples / geturl.rexx < prev    next >
OS/2 REXX Batch file  |  1999-05-25  |  3KB  |  108 lines

  1. /*************************************************
  2.  
  3.    GetURL.rexx
  4.  
  5.    Author: Håkan Parting (hparting@hem.passagen.se)
  6.    
  7.    An ARexx implementation of GetURL.c.  Works pretty much
  8.    the same as the C version does!  Requires amarquee.library
  9.    v49 and rexxamarquee.library V49 or higher.
  10.  
  11.    Usage:  rx geturl.rexx [servername] [portNumber]
  12.    
  13. ***************************************************/
  14.  
  15. parse arg serverName portNum .
  16.  
  17. if (serverName == '?') then do
  18.   say "Usage: rx geturl.rexx [serverName] [portNum]"
  19.   say "e.g. rx geturl.rexx 'ACS.hostile.cx' 80"
  20.   say "       (defaults args are localhost 80)"
  21.   exit
  22. end
  23.   
  24. if (length(serverName) = 0) then serverName = 'localhost'
  25. if (length(portNum) = 0)    then portNum    = 80
  26.  
  27. /* We need to trap all the different ways the script could exit,
  28.    so that we can be sure any allocated QSessions or QMessages are
  29.    freed properly */
  30. signal on error
  31. signal on syntax
  32. signal on halt
  33. signal on break_c
  34.  
  35. /* Used to track allocated QSession and QMessage */
  36. session = 0
  37. message = 0
  38. terminate = 0
  39. NL = '0a'x
  40.  
  41. /* Note that we require rexxamarquee.library v49 or higher */
  42. /* Also note that the liboffset must be -30 */
  43. check = addlib('rexxamarquee.library', 0, -30, 49)
  44.  
  45. /* Here's where we connect to the server... */
  46.  
  47. say "Connecting to server " || serverName || " on port " || portNum
  48. session = QNewSocketSession(serverName, portNum)
  49. if (session > 0) then do
  50.     say "Connection successful."
  51.     say "Retrieving index.html at " || serverName
  52.     res=QSendRawOp(session,"GET / HTTP/1.0" || NL || NL)
  53.     call QGo(session)
  54.   
  55. MainLoop:
  56.     do (terminate~=1)
  57.     /* This will block until we get a signal (e.g. CTRL-C) or a QMessage
  58.        arrives over our connection. */
  59.         message = GetNextQMessage(session, -1, 'SIGBREAKF_CTRL_C')
  60.     
  61.         if (message > 0) then do
  62.  
  63.             if GetQMessageField(message, 'Status') ~= QERROR_NO_ERROR then do
  64.                 say "Connection closed."
  65.                 terminate=1
  66.                 /* We will exit right after this */
  67.             end
  68.             else
  69.             do
  70.                 /* Print out the web page */
  71.                 say "**** PACKET START ****"
  72.                 say GetQMessageField(message, 'Data')
  73.                 say "**** PACKET END ****"
  74.             end
  75.  
  76.             call FreeQMessage(session, message)
  77.             message = 0
  78.         end /* (message > 0) */
  79.         else
  80.         do
  81.             say "GetNextQMessage returned NULL... probably because we caught a signal."
  82.         end
  83.       end /* (terminate=0) */
  84. end /* (session > 0) */
  85. else say "Couldn't connect to server, sorry."
  86.  
  87. /* cleanup sesssion */
  88. if (session > 0) then do
  89.     call QFreeSession(session)
  90. end
  91. EXIT
  92.  
  93. /* Our error handling/cleanup routine starts here */
  94. ERROR:
  95. SYNTAX:
  96. HALT:
  97. BREAK_C:
  98.   say "CTRL-C or error detected in line " || sigl
  99.   if (message > 0) then do
  100.     call FreeQMessage(session, message)
  101.     end
  102.   if (session > 0) then do
  103.     call QFreeSession(session)
  104.     end
  105.   exit
  106.  
  107.  
  108.